import globals from 'globals';
import pluginJs from '@eslint/js';
import pluginReact from 'eslint-plugin-react';
import babelParser from '@babel/eslint-parser';
import pluginPrettier from 'eslint-plugin-prettier';
import configPrettier from 'eslint-config-prettier';
import reactHooksPlugin from 'eslint-plugin-react-hooks';

export default [
    {
        ignores: [
            'node_modules/**',
            'dist/**',
            'build/**',
            'public/**',
            '*.config.js',
            '*.config.mjs',
            '.prettierrc',
            '.git/**',
            'coverage/**',
            'resource/map/js/**',
            'scripts/**',
        ],
    },
    pluginJs.configs.recommended,
    pluginReact.configs.flat.recommended,
    configPrettier,
    {
        files: ['**/*.{js,jsx}'],
        languageOptions: {
            parser: babelParser,
            parserOptions: {
                ecmaFeatures: { jsx: true },
                sourceType: 'module',
                requireConfigFile: false,
                babelOptions: { presets: ['@babel/preset-react'] },
                ecmaVersion: 'latest',
            },
            globals: {
                ...globals.browser,
                ...globals.node,
                React: 'writable',
                define: 'writable',
            },
        },
        plugins: {
            react: pluginReact,
            'react-hooks': reactHooksPlugin,
            prettier: pluginPrettier,
        },
        rules: {
            'linebreak-style': 'off',
            quotes: ['error', 'single', { avoidEscape: true, allowTemplateLiterals: true }],
            indent: 'off',
            semi: ['error', 'always'],
            eqeqeq: ['error', 'always'],
            'no-dupe-keys': 'error',
            'prettier/prettier': [
                'error',
                {
                    configFile: './.prettierrc',
                    endOfLine: 'lf',
                    tabWidth: 4,
                    useTabs: false,
                    singleQuote: true,
                    semi: true,
                    printWidth: 120,
                    trailingComma: 'es5',
                    arrowParens: 'always',
                    jsxSingleQuote: false,
                    bracketSpacing: true,
                    bracketSameLine: false,
                },
            ],
            'space-before-function-paren': 'off',
            'space-infix-ops': 'off',
            'object-curly-spacing': 'off',
            'arrow-spacing': 'off',
            'comma-spacing': 'off',
            'key-spacing': 'off',
            'react/prop-types': 'warn',
            'no-self-assign': 'warn',
            'no-unused-vars': [
                'warn',
                {
                    vars: 'all',
                    varsIgnorePattern: '.*',
                    args: 'all',
                    argsIgnorePattern: '.*',
                    caughtErrorsIgnorePattern: '.*',
                },
            ],
            'react/jsx-key': 'warn',
            'no-unsafe-optional-chaining': 'warn',
            'no-constant-binary-expression': 'warn',
            'no-console': ['warn', { allow: ['log', 'warn', 'error'] }],
            'react-hooks/rules-of-hooks': 'error',
            'react-hooks/exhaustive-deps': 'warn',
            'react/react-in-jsx-scope': 'off',
            'react/jsx-uses-react': 'off',
        },
        settings: {
            react: { version: 'detect' },
        },
    },
];
